<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Android extends CI_Controller{
	public function image(){
        $path = "./image/";
        foreach($_FILES['file']['name'] as $key=>$file) 
        {
            $filename = $_FILES['file']['name'][$key];
            $filepath = $_FILES['file']['tmp_name'][$key];
            $filename = $path.$filename;
            
            if(move_uploaded_file($filepath, $filename)) {  
               $result = $result.$key;
            }else{  
               
            }
        }

        echo $result;
    }
    public function login(){
    	header("Content-Type: text/html; charset=utf-8");
		session_start();
		$username = htmlspecialchars($_POST['username']);
		$password = $_POST['password'];
		//该数组主要是格式化数据并封装成json格式发送到客户端
		$arr=array();
		// $userinfo = $this->admin_model->get_login_info_by_username($username);
		$userinfo = $this->android_model->get_user_by_android_username($username);
		if($userinfo && $userinfo['passwd']==md5(md5(md5($password))))
		{
			//用户名、密码正确
			$_SESSION['username']=$username;
			//获取当前session的id
			$_SESSION['sessionid']=session_id();
			$arr =array(
				'flag'=>'success',
				'name'=>$_SESSION['username'],
				'sessionid'=>$_SESSION['sessionid']
			);
			echo json_encode($arr);
		}
		else{
			$arr =array(
				'flag'=>'error',
				'name'=>'',
				'sessionid'=>''
			);
			echo json_encode($arr);
		}
    }
    public function receivefile(){
    	$target_path  = "./upload/";//接收文件目录  
		$extend =explode("." ,basename( $_FILES['uploadedfile']['name'])); 
		$va=count($extend)-1; //获取后缀名
		$na=count($extend)-2;//获取文件名
		$last_name=explode("@", $extend[$na]);
		$arr_last_name=count($last_name)-2;//获取文件名@字符前面的部分
		$arr_next_name=count($last_name)-1;//获取文件名@字符后面的部分
	    //上传文件的具体存储位置；
	    $target_path = $target_path.$last_name[$arr_last_name]."/".$last_name[$arr_next_name].".".$extend[$va];
	    if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) { 
	       // echo "The file ".  basename( $_FILES['uploadedfile']['name']). " has been uploaded";  
	    	echo "短信备份成功";
	    }  else{  
	       // echo "There was an error uploading the file, please try again!" . $_FILES['uploadedfile']['error'];  
	    	echo "短信备份失败";
	    }
    }
    public function registered(){
    	header("Content-Type: text/html; charset=utf-8");
		session_start();
		$username = $_POST['username'];
		$password = $_POST['password'];
		$path = "./upload/".$username;//接收文件目录
		//该数组主要是格式化数据并封装成json格式发送到客户端
		$arr=array();
		$userinfo = $this->android_model->add_android_username($username,$password);
		if($userinfo)
		{
			//用户名、密码正确

			if(file_exists($path)){//如果文件已经存在、证明该用户已经注册过了
				$arr =array(
					'flag'=>'exit',
					'name'=>'',
					'sessionid'=>''
				);
			}
			else{
				mkdir($path);
			}

			$_SESSION['username']=$username;
			//获取当前session的id
			$_SESSION['sessionid']=session_id();
			$arr =array(
				'flag'=>'success',
				'name'=>$_SESSION['username'],
				'sessionid'=>$_SESSION['sessionid']
			);
			echo json_encode($arr);
		}
		else{
			$arr =array(
				'flag'=>'error',
				'name'=>'',
				'sessionid'=>''
			);
			echo json_encode($arr);
		}
	}
}
?>